Skip to content

only apply mask to _led_buffer if < 256 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 17, 2018
Merged

only apply mask to _led_buffer if < 256 #9

merged 2 commits into from
Oct 17, 2018

Conversation

brennen
Copy link
Contributor

@brennen brennen commented Oct 11, 2018

This works on CPython with a single Trellis board. I think it will work on CircuitPython, since the old behavior is effectively a no-op there. I'll test shortly. On CPython it was throwing a ValueError:

(.env) pi@raspberrypi:~/Adafruit_CircuitPython_Trellis $ python3 examples/trellis_simpletest.py 
Turning all LEDs on...
Turning all LEDs off...
Turning on each LED, one at a time...
Traceback (most recent call last):
  File "examples/trellis_simpletest.py", line 37, in <module>
    trellis.led[i] = True
  File "/home/pi/Adafruit_CircuitPython_Trellis/adafruit_trellis.py", line 94, in __setitem__
    self._parent._led_buffer[x // 16][(led * 2) + 1] |= mask
ValueError: byte must be in range(0, 256)

(The byte in question is definitely mask.)

I want to be clear that I don't understand this code, so I'm definitely not confident that this change is the Correct Thing.

This works on CPython.  I _think_ it will work on CircuitPython, since
the old behavior is effectively a no-op there.  On CPython it was
throwing a `ValueError`.

I want to be clear that I don't understand this code, so I'm definitely
not confident that this change is the Correct Thing.
@brennen brennen requested review from sommersoft and a team October 11, 2018 23:24
@sommersoft
Copy link
Collaborator

sommersoft commented Oct 12, 2018

Well, for starters, you are correct in that this seems to be a difference in behavior between CPython and CircuitPython. CPython docs clearly state:

While bytes literals and representations are based on ASCII text, bytes objects actually behave like immutable sequences of integers, with each value in the sequence restricted such that 0 <= x < 256 (attempts to violate this restriction will trigger ValueError).

My guess is that on the C-side of CircuitPython, MP_OBJ_SMALL_INT is turning anything over 256 into 1. But, that is a major off-the-cuff guess.

Truth be told, I had a hard time porting this library over from Arduino to begin with. My bitwise game is not that good (so it was a learning experience). And with that, I didn't even recognize its incompatibility with CPython bytes objects. I shoe-stringed my way through it using the Arduino lib and the HT16K33 CircuitPython lib and datasheet. I realized that the ledLUT and buttonLUT were required since the Trellis uses a "non-sequential" setup. Those were copied straight from the Arduino library.

I tested your change on CircuitPython with a Metro M4 and 2 Trellis boards (32 LEDs/buttons). It works the same as pre-change, so I approve of the change since it makes this CPython/Blinka compatible. However, I would rather have someone else review and approve, in case there is a better way. We'll still be bound to using bytearrays with busio.I2C...so I'm not sure how much "improvement" we'll get.

@brennen
Copy link
Contributor Author

brennen commented Oct 12, 2018

My bitwise game is not that good

Me either, to put it mildly. Thanks for the check and the testing.

@ladyada, thoughts?

@@ -91,7 +91,8 @@ def __setitem__(self, x, value):
led = ledLUT[x % 16] >> 4
mask = 1 << (ledLUT[x % 16] & 0x0f)
if value:
self._parent._led_buffer[x // 16][(led * 2) + 1] |= mask
if mask < 256:
self._parent._led_buffer[x // 16][(led * 2) + 1] |= mask
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will fix it because you know above that only one bit is set. However, its probably simpler this way:

self._parent._led_buffer[x // 16][(led * 2) + 1] |= (mask & 0xff)

This only allows bits in the first byte (0xff) to be preserved. The next line below drops those bits by shifting them off the right side.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@tannewt tannewt merged commit 4932de9 into master Oct 17, 2018
tannewt pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Nov 13, 2018
Updating https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15 to 0.5.3 from 0.5.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_ADS1x15#18 from adafruit/pypi-readme-update

Updating https://github.com/adafruit/Adafruit_CircuitPython_AMG to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc
  > Update rpi_thermal_cam.py
  > Update rpi_thermal_cam.py
  > Update rpi_thermal_cam.py
  > Merge pull request adafruit/Adafruit_CircuitPython_AMG#9 from kattni/example-update
  > DM: my man pylint
  > DM: make travis happy
  > DM: add blinka thermal cam example

Updating https://github.com/adafruit/Adafruit_CircuitPython_APDS to 1.1.2 from 1.1.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_APDS#9 from adafruit/pypi-readme-update
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_AS726x to 1.0.3 from 1.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_AS726x#5 from adafruit/pypi-readme-update
  > ignore the board module imports in .pylintrc
  > Merge pull request adafruit/Adafruit_CircuitPython_AS726x#4 from adafruit/pypi_readme

Updating https://github.com/adafruit/Adafruit_CircuitPython_BME280 to 2.2.0 from 2.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_BME280#16 from adafruit/pypi-readme-update
  > ignore the board module imports in .pylintrc
  > Merge pull request adafruit/Adafruit_CircuitPython_BME280#13 from adafruit/revert-12-add-dew-point

Updating https://github.com/adafruit/Adafruit_CircuitPython_BME680 to 3.0.6 from 3.0.5:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_BMP280 to 3.0.9 from 3.0.8:
  > Merge pull request adafruit/Adafruit_CircuitPython_BMP280#10 from adafruit/pypi-readme-update

Updating https://github.com/adafruit/Adafruit_CircuitPython_CCS811 to 1.1.3 from 1.1.2:
  > ignore the board module imports in .pylintrc
  > Merge pull request adafruit/Adafruit_CircuitPython_CCS811#24 from kattni/update-example
  > Merge pull request adafruit/Adafruit_CircuitPython_CCS811#23 from adafruit/fix_module_caps

Updating https://github.com/adafruit/Adafruit_CircuitPython_CharLCD to 2.4.1 from 2.4.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_CharLCD#17 from adafruit/rgb_simpletest_pins

Updating https://github.com/adafruit/Adafruit_CircuitPython_DRV2605 to 1.0.0 from 0.9.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_DRV2605#11 from process1183/sequence_property
  > Merge pull request adafruit/Adafruit_CircuitPython_DRV2605#10 from process1183/asserts
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_DS1307 to 1.3.1 from 1.3.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_DS18X20 to 1.1.2 from 1.1.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_DS18X20#8 from adafruit/setup-py-fix

Updating https://github.com/adafruit/Adafruit_CircuitPython_DS2413 to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_DS3231 to 2.1.1 from 2.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_DS3231#10 from process1183/blinka
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_EPD to 1.2.0 from 1.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_EPD#4 from adafruit/tannewt-patch-1
  > ignore the board module imports in .pylintrc
  > DM: slight example fix
  > dearest pylint
  > DM: to my friend pylint
  > DM: fix pylint stuff
  > DM: working blinka example
  > DM: adding blinka support

Updating https://github.com/adafruit/Adafruit_CircuitPython_Fingerp to 1.1.2 from 1.1.1:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_FocalTouch to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_FXAS21002C to 1.2.1 from 1.2.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_FXOS8700 to 1.2.1 from 1.2.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_GPS to 3.2.0 from 3.1.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_GPS#13 from kattni/example-name-change
  > Merge pull request adafruit/Adafruit_CircuitPython_GPS#11 from process1183/blinka
  > Merge pull request adafruit/Adafruit_CircuitPython_GPS#9 from arofarn/master
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_INA219 to 3.1.1 from 3.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_IS31FL3731 to 2.2.1 from 2.2.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_LSM303 to 1.2.2 from 1.2.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_LSM303#9 from adafruit/microbuilder-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_MatrixKe to 1.1.3 from 1.1.2:
  > Rename file
  > Merge pull request adafruit/Adafruit_CircuitPython_MatrixKe#5 from adafruit/rpi_simpletests

Updating https://github.com/adafruit/Adafruit_CircuitPython_MAX31855 to 3.0.4 from 3.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_MAX31855#8 from sommersoft/pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_MAX31865 to 2.1.1 from 2.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_MAX9744 to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_MCP4725 to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_MCP9808 to 3.2.1 from 3.2.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_MLX90393 to 1.1.2 from 1.1.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_MLX90393#3 from adafruit/setup-py-update

Updating https://github.com/adafruit/Adafruit_CircuitPython_MMA8451 to 1.2.1 from 1.2.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_MPL115A2 to 1.0.1 from 1.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_MPL115A2#2 from kattni/pypi
  > Removing initial readme.

Updating https://github.com/adafruit/Adafruit_CircuitPython_MPL3115A2 to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_MPR121 to 1.2.1 from 1.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_MPR121#13 from caternuson/iss11
  > Merge pull request adafruit/Adafruit_CircuitPython_MPR121#10 from caternuson/example_update

Updating https://github.com/adafruit/Adafruit_CircuitPython_NeoTre to 1.0.3 from 1.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_NeoTre#3 from adafruit/build-badge-update

Updating https://github.com/adafruit/Adafruit_CircuitPython_PCA9685 to 3.2.3 from 3.2.2:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_PCF8523 to 1.2.1 from 1.2.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_Pixie to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_RFM69 to 1.2.1 from 1.2.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display to 3.1.1 from 3.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_SGP30 to 2.0.0 from 1.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_SGP30#12 from adafruit/pypi-readme-update
  > Merge pull request adafruit/Adafruit_CircuitPython_SGP30#11 from process1183/typos
  > Merge pull request adafruit/Adafruit_CircuitPython_SGP30#10 from process1183/standardize
  > Merge pull request adafruit/Adafruit_CircuitPython_SGP30#9 from brentru/patch-1
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_SH to 2.0.1 from 2.0.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_SI4713 to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc
  > Merge pull request adafruit/Adafruit_CircuitPython_SI4713#5 from adafruit/raspi_fixen

Updating https://github.com/adafruit/Adafruit_CircuitPython_SI5351 to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_SI7021 to 3.1.1 from 3.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_SSD1306 to 2.4.1 from 2.4.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_STMPE610 to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_TLC5947 to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc
  > Merge pull request adafruit/Adafruit_CircuitPython_TLC5947#5 from adafruit/loop_simpletest
  > Merge pull request adafruit/Adafruit_CircuitPython_TLC5947#4 from adafruit/led_up_down_example

Updating https://github.com/adafruit/Adafruit_CircuitPython_TLC59711 to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_TMP007 to 1.0.1 from 1.0.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_Trellis to 1.2.0 from 1.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_Trellis#9 from adafruit/cpython_mask
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_TrellisM4 to 1.2.1 from 1.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_TrellisM4#13 from kattni/example-rename
  > Merge pull request adafruit/Adafruit_CircuitPython_TrellisM4#12 from kattni/neopixel-demo-code

Updating https://github.com/adafruit/Adafruit_CircuitPython_TSL2561 to 3.1.2 from 3.1.1:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_TSL2591 to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc
  > Merge pull request adafruit/Adafruit_CircuitPython_TSL2591#6 from Rvice/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_VCNL4010 to 0.9.1 from 0.9.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_VEML6070 to 2.0.0 from 1.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_VEML6070#5 from sommersoft/prop_set
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_VL53L0X to 3.1.4 from 3.1.3:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_VL6180X to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_WS2801 to 0.9.1 from 0.9.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_AVRprog to 1.1.1 from 1.1.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_FancyLED to 1.2.1 from 1.2.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing to 0.9.3 from 0.9.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_FeatherWing#16 from process1183/blinka
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation to 0.8.1 from 0.8.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_LED_Animation#1 from siddacious/master
  > fixes rtd badge url.

Updating https://github.com/adafruit/Adafruit_CircuitPython_OneWire to 1.1.1 from 1.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_OneWire#9 from caternuson/iss7_max_device
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_Register to 1.3.2 from 1.3.1:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_Waveform to 1.2.1 from 1.2.0:
  > ignore the board module imports in .pylintrc

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_DS18X20, Adafruit_CircuitPython_MLX90393, Adafruit_CircuitPython_MPL115A2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants